package com.huayi.website.controller.manage;

import com.huayi.website.entity.ProductEntity;
import com.huayi.website.request.GoodsAddVo;
import com.huayi.website.request.GoodsDeleteReq;
import com.huayi.website.service.ProductService;
import com.huayi.website.utils.EnumResultVo;
import com.huayi.website.utils.PagingResponse;
import com.huayi.website.utils.ResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

@RequestMapping("/manage")
@Controller
public class MProductController {
    @Autowired
    private ProductService productService;
    /**
     * 列表
     *
     * @return
     */
    @RequestMapping(value = "/product_list", method = RequestMethod.GET)
    public String getList(HttpServletRequest request, Model model) {
        Integer category = null;
        String title = null;
        Integer pageIndex = 1, pageSize = 10;
        if (!StringUtils.isEmpty(request.getParameter("page"))) {
            pageIndex = Integer.parseInt(request.getParameter("page"));
        }
        if (!StringUtils.isEmpty(request.getParameter("category"))) {
            category = Integer.parseInt(request.getParameter("category"));
            model.addAttribute("category",category);
        }
        if (!StringUtils.isEmpty(request.getParameter("title"))) {
            title = request.getParameter("title");
            model.addAttribute("title",title);
        }


        model.addAttribute("pageIndex", pageIndex);
        model.addAttribute("pageSize", pageSize);
        PagingResponse<ProductEntity> result = productService.getAllList(pageIndex, pageSize, title,category,null);
        model.addAttribute("list", result.getList());
        model.addAttribute("totalSize", result.getTotalSize());

        var categoryList = productService.getAllCategoryList();
        model.addAttribute("categoryList", categoryList);

        model.addAttribute("menu","product_list");
        return "manage/product_list";
    }

    /**
     * 添加商品
     * @param request
     * @param model
     * @return
     */
    @RequestMapping(value = "/product_add", method = RequestMethod.GET)
    public String add(HttpServletRequest request, Model model) {

        var categoryList = productService.getAllCategoryList();
        model.addAttribute("categoryList", categoryList);

        model.addAttribute("menu","product_list");
        return "manage/product_add";
    }

    /**
     * 添加商品
     * @param request
     * @param model
     * @return
     */
    @RequestMapping(value = "/product_edit", method = RequestMethod.GET)
    public String edit(HttpServletRequest request, @RequestParam Integer id, Model model) {
        var detailVo = productService.getDetailById(id);
        if(detailVo==null) return "redirect:/manage/product_list";


        model.addAttribute("product", detailVo);
        model.addAttribute("categoryId", detailVo.getRefId());

        //商品图片
        if (detailVo.getImages() != null) {
            if (detailVo.getImages().size() > 0 && StringUtils.isEmpty(detailVo.getImages().get(0).getUrl()) == false)
                model.addAttribute("image1", detailVo.getImages().get(0).getUrl());
            else model.addAttribute("image1","");

            if (detailVo.getImages().size() > 1 && StringUtils.isEmpty(detailVo.getImages().get(1).getUrl()) == false)
                model.addAttribute("image2", detailVo.getImages().get(1).getUrl());
            else model.addAttribute("image2","");

            if (detailVo.getImages().size() > 2 && StringUtils.isEmpty(detailVo.getImages().get(2).getUrl()) == false)
                model.addAttribute("image3", detailVo.getImages().get(2).getUrl());
            else model.addAttribute("image3","");

            if (detailVo.getImages().size() > 3 && StringUtils.isEmpty(detailVo.getImages().get(3).getUrl()) == false)
                model.addAttribute("image4", detailVo.getImages().get(3).getUrl());
            else model.addAttribute("image4","");
        }

        if (detailVo.getDetailImages()!=null) {
            if (detailVo.getDetailImages().size() > 0 && StringUtils.isEmpty(detailVo.getDetailImages().get(0).getUrl()) == false)
                model.addAttribute("detailImage1", detailVo.getDetailImages().get(0).getUrl());

            if (detailVo.getDetailImages().size() > 1 && StringUtils.isEmpty(detailVo.getDetailImages().get(1).getUrl()) == false)
                model.addAttribute("detailImage2", detailVo.getDetailImages().get(1).getUrl());

            if (detailVo.getDetailImages().size() > 2 && StringUtils.isEmpty(detailVo.getDetailImages().get(2).getUrl()) == false)
                model.addAttribute("detailImage3", detailVo.getDetailImages().get(2).getUrl());

            if (detailVo.getDetailImages().size() > 3 && StringUtils.isEmpty(detailVo.getDetailImages().get(3).getUrl()) == false)
                model.addAttribute("detailImage4", detailVo.getDetailImages().get(3).getUrl());

            if (detailVo.getDetailImages().size() > 4 && StringUtils.isEmpty(detailVo.getDetailImages().get(4).getUrl()) == false)
                model.addAttribute("detailImage5", detailVo.getDetailImages().get(4).getUrl());

            if (detailVo.getDetailImages().size() > 5 && StringUtils.isEmpty(detailVo.getDetailImages().get(5).getUrl()) == false)
                model.addAttribute("detailImage6", detailVo.getDetailImages().get(5).getUrl());

            if (detailVo.getDetailImages().size() > 6 && StringUtils.isEmpty(detailVo.getDetailImages().get(6).getUrl()) == false)
                model.addAttribute("detailImage7", detailVo.getDetailImages().get(6).getUrl());

            if (detailVo.getDetailImages().size() > 7 && StringUtils.isEmpty(detailVo.getDetailImages().get(7).getUrl()) == false)
                model.addAttribute("detailImage8", detailVo.getDetailImages().get(7).getUrl());

            if (detailVo.getDetailImages().size() > 8 && StringUtils.isEmpty(detailVo.getDetailImages().get(8).getUrl()) == false)
                model.addAttribute("detailImage9", detailVo.getDetailImages().get(8).getUrl());

            if (detailVo.getDetailImages().size() > 9 && StringUtils.isEmpty(detailVo.getDetailImages().get(9).getUrl()) == false)
                model.addAttribute("detailImage10", detailVo.getDetailImages().get(9).getUrl());

        }



        var categoryList = productService.getAllCategoryList();
        model.addAttribute("categoryList", categoryList);


        model.addAttribute("menu","product_list");
        return "manage/product_edit";
    }
    /**
     * 商品发布AJAX
     *
     * @param model
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/product_add_submit", method = RequestMethod.POST)
    public ResultVo<String> publishPost(Model model, HttpServletRequest request) {
        HttpSession session = request.getSession();
        String userName = session.getAttribute("userName").toString();
        if (StringUtils.isEmpty(userName)) return new ResultVo<>(500, "请登录");


        String categoryId = request.getParameter("categoryId");
        String title = request.getParameter("title");
        String etitle = request.getParameter("etitle");
        String detail = request.getParameter("detail");
        String edetail = request.getParameter("edetail");
        String sort = request.getParameter("sort");
        String number = request.getParameter("number");
        Integer companyId = Integer.parseInt(request.getParameter("companyId"));

        //商品添加VO
        GoodsAddVo goodsAddVo = new GoodsAddVo();
        goodsAddVo.setCategoryId(Integer.parseInt(categoryId));
        goodsAddVo.setTitle(title);
        goodsAddVo.setEtitle(etitle);
        goodsAddVo.setCompanyId(companyId);
        goodsAddVo.setNumber(number);
        goodsAddVo.setDetailText(detail);
        goodsAddVo.setDetaileText(edetail);
        try {
            goodsAddVo.setSort(Integer.parseInt(sort));
        }catch (Exception e){}
        //图片
        String[] images = request.getParameterValues("image");
        if (images == null || images.length == 0 || StringUtils.isEmpty(images[0])) {
            //商品主图不能为空
            return new ResultVo<>(200, "商品主图不能为空");
        } else {
            goodsAddVo.setImage(images[0]);
            goodsAddVo.setImages(images);
        }

        //视频
        String videoUrl = request.getParameter("video");
        goodsAddVo.setVideo(videoUrl);

        //商品详情图片
        String[] detailImages = request.getParameterValues("detail_image");
        goodsAddVo.setDetailImages(detailImages);


        Integer goodsId = productService.addGoods(goodsAddVo, userName);



        return new ResultVo<>(0, "SUCCESS");
    }


    /**
     * 商品发布AJAX
     *
     * @param model
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/product_edit_submit", method = RequestMethod.POST)
    public ResultVo<String> editPost(Model model, HttpServletRequest request) {
        HttpSession session = request.getSession();
        String userName = session.getAttribute("userName").toString();
        if (StringUtils.isEmpty(userName)) return new ResultVo<>(500, "请登录");


        Integer productId = Integer.parseInt(request.getParameter("productId"));

        String categoryId = request.getParameter("categoryId");
        String title = request.getParameter("title");
        String etitle = request.getParameter("etitle");
        String detail = request.getParameter("detail");
        String edetail = request.getParameter("edetail");
        String sort = request.getParameter("sort");
        Integer companyId = Integer.parseInt(request.getParameter("companyId"));

        String number = request.getParameter("number");

        //商品添加VO
        GoodsAddVo goodsAddVo = new GoodsAddVo();
        goodsAddVo.setCategoryId(Integer.parseInt(categoryId));
        goodsAddVo.setTitle(title);
        goodsAddVo.setEtitle(etitle);
        goodsAddVo.setCompanyId(companyId);
        goodsAddVo.setDetailText(detail);
        goodsAddVo.setDetaileText(edetail);
        goodsAddVo.setNumber(number);
        try {
            goodsAddVo.setSort(Integer.parseInt(sort));
        }catch (Exception e){}

        //图片
        String[] images = request.getParameterValues("image");
        if (images == null || images.length == 0 || StringUtils.isEmpty(images[0])) {
            //商品主图不能为空
            return new ResultVo<>(200, "商品主图不能为空");
        } else {
            goodsAddVo.setImage(images[0]);
            goodsAddVo.setImages(images);
        }

        //视频
        String videoUrl = request.getParameter("video");
        goodsAddVo.setVideo(videoUrl);

        //商品详情图片
        String[] detailImages = request.getParameterValues("detail_image");
        goodsAddVo.setDetailImages(detailImages);


        Integer goodsId = productService.editGoods(productId,goodsAddVo, userName);



        return new ResultVo<>(0, "SUCCESS");
    }

    /**
     * 删除
     *
     * @param req
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/product_delete", method = RequestMethod.POST)
    public ResultVo<Integer> delete(@RequestBody GoodsDeleteReq req) {
        if (req.getId() == null || req.getId() <= 0)
            return new ResultVo<>(EnumResultVo.ParamsError.getIndex(), "参数错误，缺少id");

        productService.deleteGoods(req.getId());

        return new ResultVo<>(EnumResultVo.SUCCESS);
    }
}
